package cn.broadcasereceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
    private IOnUpdateListener listener;

    public MyBroadcastReceiver(IOnUpdateListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MyBroadcastReceiver", "onReceive " + intent.getAction());

        int state = NetUtil.getNetState(context);
        switch (state) {
            case NetUtil.NET_MOBI:
                Toast.makeText(context, "移动网络可用", Toast.LENGTH_SHORT).show();
                break;
            case NetUtil.NET_WIFI:
                Toast.makeText(context, "WIFI可用", Toast.LENGTH_SHORT).show();
                break;
            case NetUtil.NET_HOME:
                Toast.makeText(context, "网络不可用", Toast.LENGTH_SHORT).show();
                break;
        }
//        时间广播
        listener.updateTime();

//        有序广播
//        Log.e("MyBroadcastReceiver", "onReceive");

//      动态注册接收天气和交通广播
//        String action = intent.getAction();
//        if (action.equals("Weather_Report")) {
//            String weather = intent.getStringExtra("Weather");
//            int temp = intent.getIntExtra("temperature", 0);
//            Log.e("MyBroadcastReceiver", "收到天气广播,天气" + weather + " 温度" + temp);
//        } else if (action.equals("Traffic_Report")) {
//            String traffic = intent.getStringExtra("Traffic");
//            int time = intent.getIntExtra("Time", 0);
//            Log.e("MyBroadcastReceiver", "收到交通广播,交通" + traffic + " 时间" + time);
//        }

    }
}
